local L = AceLibrary("AceLocale-2.2"):new("DrDamage")

DrDamage.PlayerAura = {}
DrDamage.TargetAura = {}
DrDamage.Consumables = {}
DrDamage.Calculation = {}

local function DrD_LoadAuras()
	local playerClass = select(2,UnitClass("player"))
	local playerHealer = (playerClass == "PRIEST") or (playerClass == "SHAMAN") or (playerClass == "PALADIN") or (playerClass == "DRUID")
	local playerCaster = (playerClass == "MAGE") or (playerClass == "PRIEST") or (playerClass == "WARLOCK")
	local playerMelee = (playerClass == "ROGUE") or (playerClass == "WARRIOR") or (playerClass == "HUNTER")
	local playerHybrid = (playerClass == "DEATHKNIGHT") or (playerClass == "DRUID") or (playerClass == "PALADIN") or (playerClass == "SHAMAN")	
	local Aura = DrDamage.PlayerAura
	local GetSpellInfo = GetSpellInfo
	
	--[[ NOTES: 
	 	School  = "Spells" means it applies to both heals and spells (in hybrid classes), not physical
	 	School = "Damage Spells" means it applies to all damaging spells (in hybrid classes), not physical
		School = "Healing" applies only to heals
		School = "Physical" applies only to physical type abilities, which is the default in melee module unless otherwise specified
		No school or spell applies to everything but healing
		Caster = true or Melee = true limits it to respective modules
	--]]
	
	--Bloodlust
	Aura[GetSpellInfo(2825)] = { School = "Spells", Mods = { ["castTime"] = function(v) return v/1.3 end }, Category = "30% haste" }
	--Heroism
	Aura[GetSpellInfo(32182)] = Aura[GetSpellInfo(2825)]
	--Heroic Presence
	Aura[GetSpellInfo(28878)] = { Value = 1, ModType = "hitPerc", }
	
	if playerCaster or playerHybrid then
	--CORE	
		--Bloodgem Infusion
		Aura[GetSpellInfo(34379)] = { School = "Damage Spells", Value = 0.05, NoManual = true }
		--Vibrant Blood
		Aura[GetSpellInfo(35329)] = { School = "Damage Spells", Value = 0.1, NoManual = true }		
		--Hysteria (needs to be divided out from multiplier)
		Aura[GetSpellInfo(49016)] = { School = "Damage Spells", Value = -1 + 1/1.2, NoManual = true }		
		--Unrelenting Assault
		Aura[GetSpellInfo(64850)] = { School = "Spells", Value = -0.25, Ranks = 2, NoManual = true }
	--MODS
		--Totem of Wrath
		Aura[GetSpellInfo(30706)] = { School = "Spells", Mods = { ["spellDmg"] = 280 } }
		--Moonkin Aura
		Aura[GetSpellInfo(24907)] = { School = "Spells", Mods = { ["critPerc"] = 5 }, Category = "+5% crit" }
		--Elemental Oath
		Aura[GetSpellInfo(53414)] = Aura[GetSpellInfo(24907)]	
		--Wrath of Air
		Aura[GetSpellInfo(3738)] = { School = "Spells", Mods = { ["castTime"] = function(v) return v/1.05 end }, Caster = true,  }
		--Power Infusion
		Aura[GetSpellInfo(10060)] = { School = "Spells", Mods = { ["castTime"] = function(v) return v/1.2 end }, Caster = true, }		
	end
	if playerHealer then
		--Nether Portal - Serenity
		Aura[GetSpellInfo(30422)] = { School = "Healing", Value = 0.05, Apps = 99, Caster = true, NoManual = true }
		--Wretching Bile
		Aura[GetSpellInfo(52527)] = { School = "Healing", Value = -0.35, Caster = true, NoManual = true }	
	end
	if playerHealer or playerCaster or playerHybrid then
		--Shadow Crash
		Aura[GetSpellInfo(63277)] = { School = "Spells", ModType = "Special", NoManual = true }
		DrDamage.Calculation[GetSpellInfo(63277)] = function( calculation )
			if calculation.healingSpell then
				calculation.dmgM = calculation.dmgM * 0.25
			else
				calculation.dmgM = calculation.dmgM * 2
			end
		end
		
	end
	
	--Target debuffs
	Aura = DrDamage.TargetAura

	--Icebound Fortitude (TODO: How to implement this reduction properly?)
	Aura[GetSpellInfo(48792)] = { Value = -0.2, NoManual = true }
	--Bone Shield
	Aura[GetSpellInfo(49222)] = { Value = -0.2, NoManual = true }
	--Blade Barrier
	Aura[GetSpellInfo(51789)] = { Value = -0.01, Ranks = 5, NoManual = true }
	--Totem of Wrath
	Aura[GetSpellInfo(57722)] = { Value = 3, ModType = "critPerc", Category = "+3% crit" }
	--Heart of the Crusader
	Aura[GetSpellInfo(20335)] = { Value = 1, Ranks = 3, ModType = "critPerc", Category = "+3% crit" }
	--Master Poisoner
	Aura[GetSpellInfo(58410)] = Aura[GetSpellInfo(20335)]
	--Focused Will
	Aura[GetSpellInfo(45234)] = { ModType = "Special", NoManual = true }
	local UnitExists = UnitExists
	local UnitIsFriend = UnitIsFriend
	DrDamage.Calculation[GetSpellInfo(45234)] = function( calculation, _, _, _, apps, _, rank )
		rank = rank and tonumber(string_match(rank,"%d+")) or 3
		apps = apps or 3
		if calculation.healingSpell then
			if not UnitExists("target") or UnitIsFriend("target","player") then
				calculation.dmgM = calculation.dmgM * (1 + (rank * 0.01 + 0.02) * apps)
			end
		elseif UnitExists("target") and not UnitIsFriend("target","player") then
			calculation.dmgM = calculation.dmgM * (1 - (rank * 0.01 + 0.01) * apps)
		end	
	end	
	
	if playerCaster or playerHybrid then
	--General
		--Curse of the Elements
		Aura[GetSpellInfo(1490)] = { School = "Damage Spells", Ranks = 5, Value = { 0.06, 0.08, 0.1, 0.1, 0.13 }, ActiveAura = "Soul Siphon", Category = "+13% dmg" }
		--Earth and Moon 
		Aura[GetSpellInfo(60431)] = { School = "Damage Spells", Ranks = 3, Value = { 0.04, 0.09, 0.13 }, Category = "+13% dmg" }
		--Ebon Plague
		Aura[GetSpellInfo(51735)] = Aura[GetSpellInfo(60431)]		
		--Improved Shadow Bolt
		Aura[GetSpellInfo(17800)] = { School = "Damage Spells", Ranks = 5, Value = 1, ModType = "critPerc", Category = "+5% target crit" }
		--Improved Scorch
		Aura[GetSpellInfo(22959)] = { School = "Damage Spells", Apps = 5, Value = 1, ModType = "critPerc", Category = "+5% target crit" }
		--Winter's Chill
		Aura[GetSpellInfo(12579)] = Aura[GetSpellInfo(22959)]	
		--Misery
		Aura[GetSpellInfo(33196)] = { School = "Damage Spells", Value = 1, Ranks = 3, ModType = "hitPerc", Category = "+3% hit" }
		--Anti-Magic Shell
		Aura[GetSpellInfo(48707)] = { School = "Damage Spells", Value = -0.75 }
	--Weapon Debuffs
		--Spell Vulnerability (from Nightfall)
		Aura[GetSpellInfo(23605)] = { School = "Damage Spells", Value = 0.15, NoManual = true }
		--Magic Disruption (Kael'thas fight Infinity Blade buff)
		Aura[GetSpellInfo(36478)] = { School = "Damage Spells", Value = 0.05, Apps = 5, NoManual = true }
	end
	if playerHealer then
	--Buffs
		--Amplify Magic
		Aura[GetSpellInfo(1008)] = { School = "Healing", ModType = "spellDmg", Ranks = 7, Value = { 16, 32, 53, 80, 96, 128, 255 } }
		--Quick Recovery
		Aura[GetSpellInfo(31245)] = { School = "Healing", Value = 0.1, Ranks = 2, NoManual = true }
		--Demon Armor
		Aura[GetSpellInfo(27260)] = { School = "Healing", Value = 0.2, }
		--Vampiric Blood
		Aura[GetSpellInfo(55233)] =  { School = "Healing", Value = 0.35, NoManual = true }
		--Divine Hymn
		Aura[GetSpellInfo(64844)] =  { School = "Healing", Value = 0.1, NoManual = true }
		--Guardian Spirit
		Aura[GetSpellInfo(47788)] =  { School = "Healing", Value = 0.4, NoManual = true }
		--Tree of Life
		Aura[GetSpellInfo(34123)] =  { School = "Healing", ModType = "Special" }
		local passive = select(2,GetSpellInfo(5420))
		DrDamage.Calculation[GetSpellInfo(34123)] = function( calculation, _, _, _, _, _, rank )
			if rank == passive then
				calculation.dmgM = calculation.dmgM * 1.06
			end
		end		
	--Debuffs
	--Player
		--Mortal Strike
		Aura[GetSpellInfo(12294)] = { School = "Healing", Value = -0.5, Caster = true, Category = "Mortal Strike", Manual = GetSpellInfo(12294) }
		--Aimed Shot
		Aura[GetSpellInfo(49050)] = Aura[GetSpellInfo(12294)]	
		--Furious Attacks
		Aura[GetSpellInfo(46910)] = { School = "Healing", Value = -0.25, Apps = 2, Caster = true, Category = "Mortal Strike", Manual = GetSpellInfo(12294) }
		--Wound Poison
		Aura[GetSpellInfo(43461)] = { School = "Healing", Value = -0.5, Caster = true, Category = "Mortal Strike", Manual = GetSpellInfo(12294) }
		--Shadow Embrace
		Aura[GetSpellInfo(32385)] = { School = "Healing", ModType = "dmgM_dot", Value = -0.01, Ranks = 5, Apps = 2, Multiply = true, Caster = true }
		
	--NPC	
		--Veil of Shadow
		Aura[GetSpellInfo(17820)] = { School = "Healing", Value = -0.75, Caster = true, Category = "Mortal Strike", NoManual = true }
		--Nether Portal - Dominance
		Aura[GetSpellInfo(30423)] = { School = "Healing", Value = -0.01, Apps = 10, Caster = true, Category = "Mortal Strike", NoManual = true }		
	end
	if playerMelee or playerHybrid then
	--Buffs
	--Player
		--Savage Combat
		Aura[GetSpellInfo(58683)] = { School = "Physical", Value = 0.02, Ranks = 2, }
		--Blood Frenzy
		Aura[GetSpellInfo(30070)] = Aura[GetSpellInfo(58683)]
		--Hemorrhage
		Aura[GetSpellInfo(16511)] = { School = "Physical", Value = { 13, 21, 29, 42, 75 }, Ranks = 5, ModType = "dmgBonus", ActiveAura = "Hemorrhage" }
		--Trauma
		Aura[GetSpellInfo(46856)] = { School = "Physical", Value = 0.15, Ranks = 2, ModType = "bleedBonus", Category = "+30% bleed", Manual = GetSpellInfo(33917) }
		--Mangle (Bear)
		Aura[GetSpellInfo(33878)] = { School = "Physical", Value = 0.3, ModType = "bleedBonus", Category = "+30% bleed", Manual = GetSpellInfo(33917) }
		--Mangle (Cat)
		Aura[GetSpellInfo(33876)] = Aura[GetSpellInfo(33878)]
	--Special	
		--Armor Disruption
		Aura[GetSpellInfo(36482)] = { School = "Physical", Value = 0.05, Apps = 5, NoManual = true }		
	--Debuffs
	--Player
		--Shadowform
		Aura[GetSpellInfo(15473)] = { School = "Physical", Value = -0.15, NoManual = true }
		--Sunder Armor
		Aura[GetSpellInfo(7386)] = { School = "Physical", Apps = 5, Value = 0.04, ModType = "armorPen", Category = "-20% Armor", Manual = GetSpellInfo(7386) }
		--Expose Armor
		Aura[GetSpellInfo(8647)] = { School = "Physical", Value = 0.2, ModType = "armorPen", Category = "-20% Armor", Manual = GetSpellInfo(7386) }
		--Acid Spit
		Aura[GetSpellInfo(55749)] = { School = "Physical", Apps = 2, Value = 0.1, ModType = "armorPen", Category = "-20% Armor", Manual = GetSpellInfo(7386) }
		--Faerie Fire
		Aura[GetSpellInfo(770)] = { School = "Physical", Value = 0.05, ModType = "armorPen", Category = "-5% Armor", Manual = GetSpellInfo(770) }
		--Faerie Fire (Feral)
		Aura[GetSpellInfo(16857)] = { School = "Physical", Value = 0.05, ModType = "armorPen", Category = "-5% Armor", Manual = GetSpellInfo(770) }
		--Sting
		Aura[GetSpellInfo(56626)] = { School = "Physical", Value = 0.05, ModType = "armorPen", Category = "-5% Armor", Manual = GetSpellInfo(770) }
	--NPC
		--Holyform
		Aura[GetSpellInfo(46565)] = { School = "Physical", Value = -0.2, NoManual = true }
	end
	
	local Consumables = DrDamage.Consumables
	if playerCaster or playerHybrid or playerHealer then
		--Flask of the Frost Wyrm
		Consumables[GetSpellInfo(53755)] = { School = "Spells", Mods = { ["spellDmg"] = 125 } }
		--Spellpower Elixir
		Consumables[GetSpellInfo(33721)] = { School = "Spells", Mods = { ["spellDmg"] = 58 } }
		--Flask of Pure Mojo
		Consumables[GetSpellInfo(54212)] = { School = "Spells", Mods = { ["manaRegen"] = 7.6 }, Caster = true, }
		--Elixir of Mighty Mageblood
		Consumables[GetItemInfo(40109) or "Elixir of Mighty Mageblood"] = { School = "Spells", Mods = { ["manaRegen"] = 4.8 }, Alt = GetSpellInfo(53764), Caster = true, }
		--Food
		Consumables[L["+46 Spell Power Food"]] = { School = "Spells", Mods = { ["spellDmg"] = 46, }, Alt = GetSpellInfo(57327) }
	end
	if playerMelee or playerHybrid then
		--Flask of Endless Rage
		Consumables[GetSpellInfo(53760)] = { School = { ["Physical"] = true, ["Ranged"] = true }, Mods = { ["AP"] = 180 }, }
		--Elixir of Wrath
		Consumables[GetSpellInfo(53746)] = { School = { ["Physical"] = true, ["Ranged"] = true }, Mods = { ["AP"] = 90 }, }
		--Elixir of Demonslaying
		Consumables[GetSpellInfo(11406)] = { School = { ["Physical"] = true, ["Ranged"] = true }, Mods = { ["AP"] = 265 }, }
		--Food
		Consumables[L["+60 AP Food"]] = { School = { ["Ranged"] = true, ["Physical"] = true }, Mods = { ["AP"] = 60 }, Alt = GetSpellInfo(57079) }
		Consumables[L["+80 AP Food"]] = { School = { ["Ranged"] = true, ["Physical"] = true }, Mods = { ["AP"] = 80 }, Alt = GetSpellInfo(57079) }
	end
	
	--TODO: Fix hit rating to hit for casters
	--Elixir of Accuracy
	Consumables[GetItemInfo(44325) or "Elixir of Accuracy"] = { School = { ["Damage Spells"] = true, ["Ranged"] = true, ["Physical"] = true }, Mods = { ["hitPerc"] = 1.372 }, Alt = GetSpellInfo(60340) }
	--Elixir of Deadly Strikes
	Consumables[GetItemInfo(44327) or "Elixir of Deadly Strikes"] = { School = { ["Spells"] = true, ["Ranged"] = true, ["Physical"] = true }, Mods = { ["critPerc"] = 0.9802, }, Alt = GetSpellInfo(60341) }
	--Food
	Consumables[L["+40 Hit Rating Food"]] = { School = { ["Damage Spells"] = true, ["Ranged"] = true, ["Physical"] = true }, Mods = { ["hitPerc"] = 1.22 }, Alt = GetSpellInfo(33263) }
	Consumables[L["+40 Critical Strike Rating Food"]] = { School = { ["Spells"] = true, ["Ranged"] = true, ["Physical"] = true }, Mods = { ["critPerc"] = 0.871 }, Alt = GetSpellInfo(33263) }
end

DrD_LoadAuras()
DrD_LoadAuras = nil